package com.ogawa.a7517.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;

public class SeniorAdjustFragment extends BaseFragment implements View.OnClickListener {
    private TextView tvSpot, tvArea, tvWhole;
    private ImageView ivWidth;
    private int[] widthImgRes = {R.drawable.img_tiaojie1
            , R.drawable.img_tiaojie2, R.drawable.img_tiaojie3};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_senior_adjust;
    }

    @Override
    public void initView(View view) {
        tvSpot = view.findViewById(R.id.tv_dingdian);
        tvSpot.setOnClickListener(this);
        tvArea = view.findViewById(R.id.tv_area);
        tvArea.setOnClickListener(this);
        tvWhole = view.findViewById(R.id.tv_whole_back);
        tvWhole.setOnClickListener(this);
        ivWidth = view.findViewById(R.id.iv_width_lev);
        view.findViewById(R.id.iv_reduce).setOnClickListener(this);
        view.findViewById(R.id.iv_add).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_dingdian) {
            activity7517.publish(Constant.Y_RANGE, "2");
        } else if (id == R.id.tv_area) {
            activity7517.publish(Constant.Y_RANGE, "1");
        } else if (id == R.id.tv_whole_back) {
            activity7517.publish(Constant.Y_RANGE, "3");
        } else if (id == R.id.iv_reduce) {
            activity7517.publish(Constant.X_STATUS, "1");
        } else if (id == R.id.iv_add) {
            activity7517.publish(Constant.X_STATUS, "2");
        }
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        if (!isInit) {
            return;
        }
        DeviceStateBean deviceStatusBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (deviceStatusBean != null) {
            DeviceStateBean.FunctionsBean.YRangeBean yRange = deviceStatusBean.getFunctions().getYRange();
            if (yRange != null) {
                int y = Integer.parseInt(yRange.getModelValue());
                tvSpot.setSelected(y == 2);
                tvArea.setSelected(y == 1);
                tvWhole.setSelected(y == 3);
            }
            DeviceStateBean.FunctionsBean.XPosBean xPosBean = deviceStatusBean.getFunctions().getxPos();
            if (xPosBean != null) {
                int x = Integer.parseInt(xPosBean.getModelValue());
                ivWidth.setImageResource(widthImgRes[Math.min(x, 2)]);
            }
        }
        else {
            stateReset();
        }
        int runningStatus = MassageArmchair.getInstance().getRunningStatus();
        if (runningStatus==0){
            stateReset();
        }
    }

    private void stateReset(){
        tvSpot.setSelected(false);
        tvArea.setSelected(false);
        tvWhole.setSelected(false);
    }
}
